package com.liuyang.homework.Fragment;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.liuyang.homework.Adapter.NewsAdapter;
import com.liuyang.homework.Bean.News;
import com.liuyang.homework.Bean.NewsFromJH;
import com.liuyang.homework.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

import static com.liuyang.homework.API.NewsApi.URL;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment {

    RecyclerView rvNews;
    NewsAdapter adapter;
    List<News> newsList;
    FloatingActionButton fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news, container, false);

        fab = (FloatingActionButton)view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GetNews();
            }
        });

        rvNews= (RecyclerView) view.findViewById(R.id.rv_news);
        rvNews.setLayoutManager(new LinearLayoutManager(getContext()));
        rvNews.setItemAnimator(new DefaultItemAnimator());

        rvNews.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0 && fab.isShown()) {
                    //fabMenu.startAnimation(FabMenu_fadIn);
                    fab.setVisibility(View.GONE);
                }
                if (dy < 0 && !fab.isShown()) {
                    // fabMenu.startAnimation(FabMenu_fadOut);
                    fab.setVisibility(View.VISIBLE);
                }
            }
        });

        newsList=new ArrayList<>();
        initData();
        return view;
    }

    private void initData() {
        Snackbar.make(rvNews, "自动获取新闻中……", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
        GetNews();

    }

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private void GetNews() {

        //get方法 访问 api URL地址
        OkHttpUtils.get().url(URL).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                //获取失败，打印日志，查找原因
                Log.d("abc",e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                //成功获取后，将内容保存到本地
                Gson gson = new Gson();
                NewsFromJH newsFromJH = gson.fromJson(response,NewsFromJH.class);
                if (newsFromJH.getError_code() == 0){
                    for (int i = 0; i < newsFromJH.getResult().getData().size(); i++) {
                        Log.d("abc",newsFromJH.getResult().getData().get(i).getUrl());
                        News news = new News();
                        news.setTitle(newsFromJH.getResult().getData().get(i).getTitle());
                        news.setSource(newsFromJH.getResult().getData().get(i).getAuthor_name());
                        news.setUrl(newsFromJH.getResult().getData().get(i).getUrl());
                        news.setCreatedAt(newsFromJH.getResult().getData().get(i).getDate());
                        news.setImage(newsFromJH.getResult().getData().get(i).getThumbnail_pic_s());

                        newsList.add(news);

                    }
                    adapter = new NewsAdapter(newsList,getContext());
                    rvNews.setAdapter(adapter);
                }else{
                    //获取失败，打印日志，查找原因
                    Snackbar.make(rvNews, newsFromJH.getReason(), Snackbar.LENGTH_LONG)
                            .setAction("知道了", null).show();
                }
            }
        });

    }

}
